package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zzc.fwzpxt.entity.Company;
import com.zzc.fwzpxt.entity.RegAddress;
import com.zzc.fwzpxt.service.IRegAddressService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 注册地址 reg_address 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-09-26
 */
@Controller
@RequestMapping("/admin/regAddress")
public class RegAddressController extends BaseController {

    @Autowired
    private IRegAddressService iRegAddressService;

    @GetMapping("/list")
    public String list() {
        return blogPath + "reg_address/list";
    }


    @ResponseBody
    @RequestMapping("/getList")
    public List<RegAddress> getList() {
        EntityWrapper<RegAddress> wrapper = new EntityWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderBy("sort", true);
        List<RegAddress> regAddresses = iRegAddressService.selectList(wrapper);
        return regAddresses;
    }

    /**
     * 添加修改
     * @param regAddress
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public JsonResult save(RegAddress regAddress) {
        if (regAddress.getId() == null) {
            return iRegAddressService.insert(regAddress) ? renderSuccess("添加成功") : renderError("添加失败");
        } else {
            return iRegAddressService.updateById(regAddress) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<RegAddress> addresses) {
        if (addresses == null) {
            return renderError("请选择数据");
        }
        addresses.forEach(a -> {
            a.setStatus(0);
        });

        return iRegAddressService.updateBatchById(addresses)? renderSuccess("删除成功") : renderError("删除失败");
    }

}
